﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml.Serialization;
using FenixAdar.Windows.Forms;

namespace FileThief
{
    /// <summary>
    /// 储存程序中所有的设置信息
    /// </summary>
    public class Settings
    {
        private static Settings settings;

        public static Settings GetSettings()
        {
            if (settings == null)
            {
                FenixAdar.Xml.XmlManager xmlm = new FenixAdar.Xml.XmlManager();
                try
                {
                    settings = xmlm.Deserialize<Settings>(@"AppSettings.xml");
                }
                catch
                {
                    MessageBox.Show("读取配置文件失败，已加载默认配置");
                    settings = new Settings();
                    SaveSettings();
                }
            }
            return settings;
        }

        public static void LoadDefaultSettings()
        {
            settings = new Settings();
        }

        public static void ReloadSettings()
        {
            FenixAdar.Xml.XmlManager xmlm = new FenixAdar.Xml.XmlManager();
            try
            {
                settings = xmlm.Deserialize<Settings>(@"AppSettings.xml");
            }
            catch
            {
                MessageBox.Show("读取配置文件失败，已加载默认配置");
                settings = new Settings();
                SaveSettings();
            }
        }

        public static void SaveSettings()
        {
            FenixAdar.Xml.XmlManager xmlm = new FenixAdar.Xml.XmlManager();
            try
            {
                xmlm.Serialiaze<Settings>(@"./AppSettings.xml", settings);
            }
            catch (Exception)
            {
                MessageBox.Show("保存配置文件失败");
            }
        }

        private Settings()
        {
            IsAutoStartCopy = false;
            IsAutoVisibleForm = false;
            VisibleFormHotkey1 = HotkeyModifiers.MOD_CONTROL;
            VisibleFormHotkey2 = Keys.F2;
            StartCopyHotkey1 = HotkeyModifiers.MOD_CONTROL;
            StartCopyHotkey2 = Keys.F3;
            StopCopyHotkey1 = HotkeyModifiers.MOD_CONTROL;
            StopCopyHotkey2 = Keys.F4;
            ExitAppHotkey1 = HotkeyModifiers.MOD_CONTROL;
            ExitAppHotkey2 = Keys.F5;

            detect = new DetectSettings();
            select = new SelectSettings();
            zip = new ZipSettings();
            copy = new CopySettings();
        }

        /// <summary>
        /// 是否开启启动程序后自动开始拷贝功能
        /// </summary>
        [XmlAttribute(AttributeName = "IsAutoStartCopy")]
        public bool IsAutoStartCopy;

        /// <summary>
        /// 是否开启启动程序后自动隐藏窗体功能
        /// </summary>
        [XmlAttribute(AttributeName = "IsAutoVisibleForm")]
        public bool IsAutoVisibleForm;

        /// <summary>
        /// 显示隐藏主控制台快捷键1
        /// </summary>
        [XmlAttribute(AttributeName = "VisibleFormHotkey1")]
        public HotkeyModifiers VisibleFormHotkey1;

        /// <summary>
        /// 显示隐藏主控制台快捷键2
        /// </summary>
        [XmlAttribute(AttributeName = "VisibleFormHotkey2")]
        public Keys VisibleFormHotkey2;

        /// <summary>
        /// 开始拷贝快捷键1
        /// </summary>
        [XmlAttribute(AttributeName = "StartCopyHotkey1")]
        public HotkeyModifiers StartCopyHotkey1;

        /// <summary>
        /// 开始拷贝快捷键2
        /// </summary>
        [XmlAttribute(AttributeName = "StartCopyHotkey2")]
        public Keys StartCopyHotkey2;

        /// <summary>
        /// 停止拷贝快捷键1
        /// </summary>
        [XmlAttribute(AttributeName = "StopCopyHotkey1")]
        public HotkeyModifiers StopCopyHotkey1;

        /// <summary>
        /// 停止拷贝快捷键2
        /// </summary>
        [XmlAttribute(AttributeName = "StopCopyHotkey2")]
        public Keys StopCopyHotkey2;

        /// <summary>
        /// 退出程序快捷键1
        /// </summary>
        [XmlAttribute(AttributeName = "ExitAppHotkey1")]
        public HotkeyModifiers ExitAppHotkey1;

        /// <summary>
        /// 退出程序快捷键2
        /// </summary>
        [XmlAttribute(AttributeName = "ExitAppHotkey2")]
        public Keys ExitAppHotkey2;


        [XmlElement(ElementName = "DetectSettings")]
        public DetectSettings detect;
        [XmlElement(ElementName = "SelectSettings")]
        public SelectSettings select;
        [XmlElement(ElementName = "ZipSettings")]
        public ZipSettings zip;
        [XmlElement(ElementName = "CopySettings")]
        public CopySettings copy;
       
    }
           
    public class DetectSettings
    {
        /// <summary>
        /// 是否开启不拷贝指定磁盘功能
        /// </summary>
        [XmlAttribute(AttributeName = "IsNoCopyMarkDrivesOn")]
        public bool IsNoCopyMarkDrivesOn;

        /// <summary>
        /// 不拷贝指定磁盘功能的过滤设置
        /// </summary>
        [XmlElement(ElementName = "NoCopyMarkDrives")]
        public StringFilter NoCopyMarkDrives;

        /// <summary>
        /// 拷贝指定磁盘类型的过滤设置：0、不限制；1、可移动存储设备；2、固定磁盘；3、网络驱动器；4、光盘设备；5、RAM 磁盘。
        /// </summary>
        [XmlElement(ElementName = "CopiedDriveType")]
        public int CopiedDriveType;

        /// <summary>
        /// 检测模块设置默认值：
        /// 1、不开启拷贝指定磁盘功能
        /// 2、不开启重新拷贝拷贝过的磁盘功能
        /// 3、不拷贝指定磁盘功能的过滤设置为使用黑名单
        /// </summary>
        public DetectSettings()
        {
            NoCopyMarkDrives = new StringFilter(); 
          
            IsNoCopyMarkDrivesOn = false;//不开启不拷贝指定磁盘功能
            CopiedDriveType = 0;//默认不做限制

            NoCopyMarkDrives.IsBlacklist = true; //不拷贝指定磁盘功能的过滤设置为使用黑名单
        }

    }

    public class SelectSettings
    {
        /// <summary>
        /// 是否开启文件后缀名过滤功能
        /// </summary>
        [XmlAttribute(AttributeName = "IsFileExtensionFilterOn")]
        public bool IsFileExtensionFilterOn;

        /// <summary>
        /// 是否开启文件名匹配过滤功能
        /// </summary>
        [XmlAttribute(AttributeName = "IsFileNameFilterOn")]
        public bool IsFileNameFilterOn;

        /// <summary>
        /// 是否开启文件占用空间大小过滤功能，以字节计算
        /// </summary>
        [XmlAttribute(AttributeName = "IsFileLengthFilterOn")]
        public bool IsFileLengthFilterOn;

        /// <summary>
        /// 文件后缀名过滤
        /// </summary>
        [XmlElement(ElementName = "FileExtensionFilter")]
        public StringFilter FileExtensionFilter;

        /// <summary>
        /// 文件名过滤
        /// </summary>
        [XmlElement(ElementName = "FileNameFilter")]
        public StringFilter FileNameFilter;

        /// <summary>
        /// 文件大小过滤
        /// </summary>
        [XmlElement(ElementName = "FileLengthFilter")]
        public FileLengthFilter FileLengthFilter;

        
        /// <summary>
        /// 默认开启文件后缀名过滤功能和文件大小过滤功能
        /// 默认不开启其他所有的过滤功能
        /// </summary>
        public SelectSettings()
        {
            FileExtensionFilter = new StringFilter();
            FileNameFilter = new StringFilter();
            FileLengthFilter = new FileLengthFilter(); 
                        
            IsFileExtensionFilterOn = true;
            IsFileNameFilterOn = false;
            IsFileLengthFilterOn = true;

            FileExtensionFilter.IsBlacklist = false;
            FileLengthFilter.IsBlacklist = true;

            FileExtensionFilter.Whitelist.Add(".doc");
            FileExtensionFilter.Whitelist.Add(".docx");
            FileExtensionFilter.Whitelist.Add(".xls");
            FileExtensionFilter.Whitelist.Add(".xlsx");
            FileExtensionFilter.Whitelist.Add(".ppt");
            FileExtensionFilter.Whitelist.Add(".pptx");

            FileLengthFilter.LessBlacklist = 0;
            FileLengthFilter.MoreBlacklist = 52428800;//默认最大为50MB

        }

    }

    public class ZipSettings
    {
        /// <summary>
        /// 是否开启压缩功能
        /// </summary>
        [XmlAttribute(AttributeName = "IsCreateZipFilesOn")]
        public bool IsCreateZipFilesOn;        

        /// <summary>
        /// 压缩分卷大小，以字节为单位
        /// </summary>
        [XmlAttribute(AttributeName = "ZipFileMaxLength")]
        public long ZipFileMaxLength;

        /// <summary>
        /// ZIP压缩强度
        /// </summary>
        [XmlAttribute(AttributeName = "ZipCompressionLevel")]
        public Ionic.Zlib.CompressionLevel ZipCompressionLevel;
        
        /// <summary>
        /// ZIP解压密码
        /// </summary>
        [XmlAttribute(AttributeName = "ZipPassword")]
        public string ZipPassword;

        /// <summary>
        /// ZIP文件存储位置
        /// </summary>
        [XmlElement(ElementName = "ZipFileDir")]
        public string ZipFileDir;

        public ZipSettings()
        {
            IsCreateZipFilesOn = true;
            ZipFileMaxLength = 52428800;
            ZipCompressionLevel = new Ionic.Zlib.CompressionLevel();
            ZipPassword = null;
            ZipFileDir = @".\Zip\";
            ZipCompressionLevel = Ionic.Zlib.CompressionLevel.BestSpeed;
        }
    }

    public class CopySettings
    {       
        /// <summary>
        /// 拷贝到磁盘的目标地址
        /// </summary>
        [XmlElement(ElementName = "DiskTargetPath")]
        public string DiskTargetPath;
               
        public CopySettings()
        {
            DiskTargetPath = @".\Copy\";
        }
    }

    public class StringFilter
    {
        /// <summary>
        /// 用黑名单或白名单来过滤
        /// </summary>
        [XmlAttribute(AttributeName = "IsBlacklist")]
        public bool IsBlacklist;
        
        /// <summary>
        /// 黑名单条目
        /// </summary>
        [XmlElement(ElementName = "Blacklist")]
        public List<string> Blacklist;

        /// <summary>
        /// 白名单条目
        /// </summary>
        [XmlElement(ElementName = "Whitelist")]
        public List<string> Whitelist;

        public StringFilter()
        {
            Blacklist = new List<string>();
            Whitelist = new List<string>();
        }
    }

    public class FileLengthFilter
    {
        /// <summary>
        /// 用黑名单或白名单来过滤
        /// </summary>
        [XmlAttribute(AttributeName = "IsBlacklist")]
        public bool IsBlacklist;

        /// <summary>
        /// 黑名单条目
        /// </summary>
        [XmlElement(ElementName = "LessBlacklist")]
        public long LessBlacklist;
        [XmlElement(ElementName = "MoreBlacklist")]
        public long MoreBlacklist;

        /// <summary>
        /// 白名单条目
        /// </summary>
        [XmlElement(ElementName = "LessWhitelist")]
        public long LessWhitelist;
        [XmlElement(ElementName = "MoreWhitelist")]
        public long MoreWhitelist;

        public FileLengthFilter()
        {
            
        }

    }
    


}
